//
//  ZJPublicSPinfoImageTableViewCell.m
//  ZJControlsClass
//
//  Created by mac on 2024/11/12.
//

#import "ZJPublicSPinfoImageTableViewCell.h"
#import "ZJPublicSPinfoImageCollectionViewCell.h"

#import "SaTMNewWatermarkPhotoVc.h"

@interface ZJPublicSPinfoImageTableViewCell ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic,strong) UILabel * titleLabel;
@property (nonatomic,strong) UICollectionView * collectionView;
@property (nonatomic,strong) NSMutableArray * dataSource;
@property (nonatomic,strong) UIView * v;



@end



@implementation ZJPublicSPinfoImageTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        
        [self configUI];
        
    }
    return self;
}


-(void)configUI{
    
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.collectionView];
    [self.contentView addSubview:self.v];
    
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).with.offset(ALIGN_WIDTH_PT(12));
        make.centerY.equalTo(self.contentView.mas_centerY);
        make.height.equalTo(@(ALIGN_HEIGHT_PT(49)));
        make.width.equalTo(@(SCREEN_WIDTH / 2 - 24));
    }];
    
    
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.contentView).with.offset(-ALIGN_WIDTH_PT(12));
        make.centerY.equalTo(self.contentView.mas_centerY);
        make.height.equalTo(@(ALIGN_HEIGHT_PT(44)));
        make.width.equalTo(@(SCREEN_WIDTH / 2 - 24));
        make.left.equalTo(self.titleLabel.mas_right).with.offset(24);
    }];
    
    [self.v mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView).with.offset(ALIGN_WIDTH_PT(12));
        make.right.equalTo(self.contentView).with.offset(-ALIGN_WIDTH_PT(12));
        make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-1);
        make.height.equalTo(@(1));
    }];
    
    
}


-(void)setParamer:(NSMutableDictionary *)paramer{
    _paramer = paramer;
    self.titleLabel.text = paramer[@"title"];
    
    NSMutableArray *array = @[].mutableCopy;
    
    // 判断是否已经解析过
    if ([paramer[@"temp"] isKindOfClass:[NSArray class]]) {
        array = paramer[@"temp"];
    } else {
        NSData *jsonData = [paramer[@"temp"] dataUsingEncoding:NSUTF8StringEncoding];
        NSError *error;
        array = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
        
        NSLog(@"=====-----444444%@",array);
        if (error) {
            NSLog(@"JSON解析出错：%@", error);
            [LUtilityTools showAlertWithTitle:@"温馨提示" message:[NSString stringWithFormat:@"JSON解析formItems出错：%@", error] buttonAction:^{
                
            }];
            return;
        }
    }
    
    [self.dataSource addObjectsFromArray:array];
    if (self.dataSource.count == 1){
        [self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).with.offset(-ALIGN_WIDTH_PT(12));
            make.centerY.equalTo(self.contentView.mas_centerY);
            make.height.equalTo(@(ALIGN_HEIGHT_PT(44)));
            make.width.equalTo(@(70));
            make.left.equalTo(self.contentView).with.offset(SCREEN_WIDTH - ALIGN_WIDTH_PT(82));
        }];
    }else{
        
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.contentView).with.offset(-ALIGN_WIDTH_PT(12));
            make.centerY.equalTo(self.contentView.mas_centerY);
            make.height.equalTo(@(ALIGN_HEIGHT_PT(44)));
            make.width.equalTo(@(ALIGN_WIDTH_PT(162)));
            make.left.equalTo(self.contentView).with.offset(SCREEN_WIDTH - ALIGN_WIDTH_PT(162));
        }];
        
    }
    
    
    
    
    
    
    [self.collectionView reloadData];
    
    
}





#pragma mark -- UICollectionViewDelegate,UICollectionViewDataSource
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZJPublicSPinfoImageCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZJPublicSPinfoImageCollectionViewCell" forIndexPath:indexPath];
    
    NSMutableDictionary *urlStr = @{}.mutableCopy;
    if ([self.dataSource[indexPath.row] isKindOfClass:[NSDictionary class]]) {
        urlStr = self.dataSource[indexPath.row];
    } else if ([self.dataSource[indexPath.row] isKindOfClass:[NSString class]]) {
        NSData *jsonData = [self.dataSource[indexPath.row] dataUsingEncoding:NSUTF8StringEncoding];
        urlStr = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:nil];
    }
    
    [cell.img sd_setImageWithURL:[NSURL URLWithString:urlStr[@"url"]] placeholderImage:[UIImage imageNamed:@"banner"]];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    SaTMNewWatermarkPhotoVc * vc = [SaTMNewWatermarkPhotoVc new];
    NSMutableArray * arr = @[].mutableCopy;
    [arr addObjectsFromArray:self.dataSource];
    [vc reloadImgWithFilesArray:arr WaterText:self.titleLabel.text Type:YES];
    [[SaViewControllerUtils currentViewController].navigationController pushViewController:vc animated:YES];
    
}



#pragma mark - item宽高
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(ALIGN_WIDTH_PT(70), ALIGN_HEIGHT_PT(44));
}
/**
 这里我用代理设置以下间距 感兴趣可以自己调整值看看差别
 */
#pragma mark - <UICollectionViewDelegateFlowLayout>
#pragma mark - Y间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return  ALIGN_WIDTH_PT(10);
}
#pragma mark - X间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return ALIGN_WIDTH_PT(10);
    
}




#pragma mark -- 懒加载


-(UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.text = @"姓名";
        _titleLabel.textColor = COLOR_RGB(153, 153, 153);
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.font = FONT14;
    }
    return _titleLabel;
}

-(UICollectionView *)collectionView{
    if (!_collectionView) {
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;// 横向滚动
        _collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.showsVerticalScrollIndicator = false;
        _collectionView.showsHorizontalScrollIndicator = false;
        _collectionView.pagingEnabled = YES;
        _collectionView.layer.cornerRadius = 10;
        _collectionView.layer.masksToBounds = YES;
        
        [_collectionView registerClass:[ZJPublicSPinfoImageCollectionViewCell class] forCellWithReuseIdentifier:@"ZJPublicSPinfoImageCollectionViewCell"];
        
    }
    
    return _collectionView;
}

-(UIView *)v{
    if (!_v) {
        _v = [UIView new];
        _v.backgroundColor = COLOR_RGB(237, 237, 237);
    }
    return _v;
}


-(NSMutableArray *)dataSource{
    if (!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}


@end
